home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / MSC2BC.ARJ / BASE_BEF.C < prev    next >
C/C++ Source or Header  |  1992-01-22  |  2KB  |  69 lines

  1. /* BASE_BEF.C - Microsoft C6.0a based heap example #1.
  2.    This program illustrates the interaction among the based
  3.    memory management functions ( _bheapseg, _bmalloc, _bfree
  4.    and _bfreeseg ), the C run-times, and standard C variables.
  5.    Typically, you would use based variables with compact, large,
  6.    or huge memory models.
  7.  
  8.    Copyright (c) 1991 Borland International. All rights reserved.
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <malloc.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. void main()
  18. {
  19.     /* Note 1 - Keep segment address for based variables in baseseg */
  20.     _segment baseseg;
  21.     /* Note 1 - based variables in baseseg */
  22.     char _based( baseseg ) *revstr, _based( baseseg ) *fwdstr;
  23.     /* Note 1 - pointers to characters within the based variables */
  24.     char _based( baseseg ) *ptrrev, _based( baseseg ) *ptrfwd;
  25.     /* Initialize a constant string */
  26.     char conststring[80] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  27.     int  lstr;
  28.  
  29.     /* Note 2 - Allocate 100 bytes in a based heap.
  30.        Put its pointer into baseseg.
  31.      */
  32.     if( (baseseg = _bheapseg( 100 )) == _NULLSEG )
  33.     /* Can't do, error exit 1 */
  34.     exit( 1 );
  35.  
  36.     /* Note 2 - Allocate based memory for two strings size (lstr+1). */
  37.     lstr = strlen( conststring );
  38.     if( ((fwdstr = _bmalloc( baseseg, lstr + 1 )) == _NULLOFF) ||
  39.     ((revstr = _bmalloc( baseseg, lstr + 1 )) == _NULLOFF) )
  40.     /* Can't do, error exit 2 */
  41.     exit( 2 );
  42.  
  43.     /* Convert upper case string to lower case string after copying
  44.        it to based memory.  Explicitly recast based pointers to
  45.        far pointers for clarity.
  46.      */
  47.     _fstrlwr( _fstrcpy( (char _far *)fwdstr,
  48.         (char _far *)conststring ) );
  49.  
  50.     /* Copy fwdstr string to revstr string in reversed order. */
  51.     for( ptrfwd = fwdstr + lstr - 1, ptrrev = revstr;
  52.         ptrrev < revstr + lstr; ptrfwd--, ptrrev++ )
  53.     *ptrrev = *ptrfwd;
  54.     *ptrrev = '\0';
  55.  
  56.     /* Note 3 - Display the strings.
  57.        Here, you must explicitly recast based pointers to far pointers
  58.        to eliminate compiler warnings.
  59.     */
  60.     printf( "Forward string:  %Fs\n", (char _far *)fwdstr );
  61.     printf( "Reverse string:  %Fs\n", (char _far *)revstr );
  62.  
  63.     /* Note 4 - Free each memory block, then release based heap. */
  64.     _bfree( baseseg, fwdstr );
  65.     _bfree( baseseg, revstr );
  66.     _bfreeseg( baseseg );
  67.     exit( 0 );
  68. }
  69.